<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class View extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    function checkLocale() {
        $query = "SELECT locale FROM user WHERE uid=me()";
        $param = array(
            'method' => 'fql.query',
            'query' => $query
        );
        $locale = $this->facebook->api($param);
        $locale = $locale[0]['locale'];
        return $locale;
    }

    function viewdefault() {
        $fb_user_id = $this->facebook->getUser();
        $products = $this->product->get_data();
        $user_voted = $this->vote->get_data_via_userid($fb_user_id);
        $data['products'] = $products;
        $data['voteds'] = $user_voted;
        $this->load->view('vote', $data);
    }
    public function viewProduct()
    {
        $fb_user_id = $this->facebook->getUser();
        $products = $this->product->get_data();
        $user_voted = $this->vote->get_data_via_userid($fb_user_id);
        $data['products'] = $products;
        $data['voteds'] = $user_voted;
        $this->load->view('vote', $data);
    }

    function viewlanding() {

        $this->load->view('landing');
    }

    function testview() {

        $this->load->view('welcome_message');
    }

    function createcover() {
        if ($this->checkLocale() == "vi_VN")
            $this->lang->load('app', 'vi');
        else {
            $this->lang->load('app', 'english');
        }
        $user = $this->facebook->api('/me');
        $access_token = $this->facebook->getAccessToken();
        $data = $this->user->getFbAlbums($this);
        $this->session->set_userdata("access_token", $access_token);
        $this->load->view('createcover', array("albums" => $data['coverPhotos'], "locale" => $this->checkLocale(), "user" => $user));
    }

    function coverresult() {
        if ($this->checkLocale() == "vi_VN")
            $this->lang->load('app', 'vi');
        else {
            $this->lang->load('app', 'english');
        }
        $user = $this->facebook->api('/me');

        $this->facebook->setFileUploadSupport(true);
        //post avatar and cover to albums
        $avatar_data = array(
            "message" => "Đây là avatar của bạn tin nhắn này chỉ bạn có thể thấy.Bạn có thể tìm thấy avatar trong albums của mình.",
            "source" => '@' . ("images/crop/" . $user['id'] . "_avatar.jpg"),
            "privacy" => array('value' => 'CUSTOM', 'friends' => "SELF")
        );
        $data['photo'] = $this->facebook->api("/me/photos", 'post', $avatar_data);

        $cover_data = array(
            "message" => "Đây là cover của bạn tin nhắn này chỉ bạn có thể thấy..Bạn có thể tìm thấy cover trong albums của mình.",
            "source" => '@' . ("images/crop/" . $user['id'] . "_cover.jpg"),
            "privacy" => array('value' => 'CUSTOM', 'friends' => "SELF")
        );

        $data['photo'] = $this->facebook->api("/me/photos", 'post', $cover_data);
        //
        $access_token = $this->facebook->getAccessToken();
        $this->session->set_userdata("access_token", $access_token);
        $this->load->view('coverresult', array("user" => $user));
    }

    function postResult() {
        $access_token = $this->session->userdata("access_token");
        $this->facebook->setAccessToken($access_token);
        $user = $this->facebook->api('/me');
        $this->facebook->setFileUploadSupport(true);
        $preview_data = array(
            "message" => $user['name'] . " đã tạo được một bộ cover tại http://apps.facebook.com/covereasydesign",
            "source" => '@' . "images/crop/" . $user['id'] . "_preview_.png",
                //'caption' => 'http://apps.facebook.com/covereasydesign', 
                //'link' => "http://apps.facebook.com/covereasydesign"
        );
        $data['photo'] = $this->facebook->api("/me/photos", 'post', $preview_data);
        $this->facebook->api("/" . $data['photo']['id'] . "/likes", 'post');
    }

    function loadalbum() {
        $access_token = $this->session->userdata("access_token");
        $this->facebook->setAccessToken($access_token);
        if (isset($_REQUEST['aid'])) {
            $photos = $this->user->getPhotoAlbums($_REQUEST['aid'], $this);
            $this->load->view('photos_ajax', array("photos" => $photos));
        } else {
            echo "-1";
        }
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */